package com.rzgeo.rzmaplib.comp;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresPermission;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.github.florent37.camerafragment.CameraFragment;
import com.github.florent37.camerafragment.configuration.Configuration;
import com.github.florent37.camerafragment.internal.ui.BaseAnncaFragment;
import com.github.florent37.camerafragment.internal.ui.view.AspectFrameLayout;
import com.github.florent37.camerafragment.internal.ui.view.AutoFitSurfaceView;
import com.github.florent37.camerafragment.internal.utils.Size;

public class RmlCameraFragment extends CameraFragment {

    @RequiresPermission(Manifest.permission.CAMERA)
    public static RmlCameraFragment newInstance(Configuration configuration) {
        return (RmlCameraFragment) BaseAnncaFragment.newInstance(new RmlCameraFragment(), configuration);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        configurationProvider.setDeviceDefaultOrientation(Configuration.ORIENTATION_LANDSCAPE);

    }

    @Override
    protected void setCameraPreview(View preview, Size previewSize) {
        super.setCameraPreview(null, previewSize);
//
//        AutoFitSurfaceView afsv = (AutoFitSurfaceView) preview;
        if (previewContainer == null || preview == null) return;
        previewContainer.removeAllViews();
        AspectFrameLayout.LayoutParams lp=new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        preview.setLayoutParams(lp);
        preview.setRotation(270);
        previewContainer.addView(preview);

        previewContainer.setAspectRatio(previewSize.getWidth()/(double) previewSize.getHeight() );
    }
}
